package ARKit;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.location.Location;
import android.util.Log;

public class MyPoint extends ARSphericalView {

	private String _name = null;
	private String _cat = null;
	private String url;
	private String ID;
	private String Address;
	private BlurMaskFilter bur;
	private float max;

	private Paint bit, p;


	public String getAddress() {
		return Address;
	}

	public void setAddress(String address) {
		Address = address;
	}

	public String getPhone() {
		return Phone;
	}

	public void setPhone(String phone) {
		Phone = phone;
	}

	private String Phone;

	public String getID() {
		return ID;
	}

	public void setID(String iD) {
		ID = iD;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String get_name() {
		return _name;
	}

	private Bitmap bitmap = null;
	private static Bitmap back = null;
	int touch_x = -100;
	int touch_y = -100;


	public MyPoint(Context ctx, Location deviceLocation,
			Location objectLocation, String name, String cat, String iD) {
		super(ctx, deviceLocation, objectLocation);
		_name = name;
		_cat = cat;
		ID = iD;
		max = 10000;
		bur = new BlurMaskFilter(250, Blur.INNER);
		bit = new Paint();
		bit.setAntiAlias(true);
	
		

		p = new Paint();
		p.setColor(Color.WHITE);
		p.setTextSize(24);
		p.setTextAlign(Align.CENTER);
		
		

		try {
			String icon_name = "i" + ID;
			int x = getResources().getIdentifier(icon_name, "drawable",
					ctx.getPackageName());
			// if bitmap is not found x is 0

			if (x == 0) {
				
				icon_name = "icon_" + _cat;
				x = getResources().getIdentifier(icon_name, "drawable",
						ctx.getPackageName());
				bitmap = BitmapFactory.decodeResource(getResources(),x);
				
			} else {
				bitmap = BitmapFactory.decodeResource(getResources(),x);
			}
			
			if(bitmap != null){
				int new_size = 120;
					new_size = 	(int) (120 - (distance / max *60));
				if(new_size < 60)
					new_size = 60;
				if(new_size > 120)
					new_size = 120;
				
				bitmap = Bitmap.createScaledBitmap(bitmap, new_size	, new_size, true);
			}

			Log.d("MyAR",
					_name + "-d-" + String.valueOf(distance) + "-- A--"
							+ String.valueOf(azimuth) + "--I--"
							+ String.valueOf(inclination));
		} catch (Exception e) {

		}
	}

	@Override
	public void draw(Canvas c) {
		super.draw(c);
		if (bitmap != null) {

			// c.drawBitmap(back, getLeft(), getTop(), bit);
			c.drawBitmap(bitmap, getLeft(), getTop(), bit);

			//c.drawBitmap(bitmap, new Rect(getLeft(), getTop(), getLeft()+100, getTop()+100), new Rect(getLeft(), getTop(), getLeft()+100, getTop()+100), bit);
			c.drawText(_name, getLeft() + (bitmap.getHeight())/2, getTop() + bitmap.getHeight() + 40, p);
//			if (collisionDetection()) {
//				c.drawBitmap(bitmap, getLeft() + 50, getTop() + 10, null);
//			}

		}
		
	}

	private boolean collisionDetection() {

		int left = getLeft();
		int top = getTop();
		int rigth = left + back.getWidth();
		int bottom = top + back.getHeight();

		if (touch_x >= left && touch_x <= rigth && touch_y >= top
				&& touch_y <= bottom)
			return true;

		return false;
	}

}
